/*
 * Copyright 2002-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.summit.serialization.httpmessage;

import cn.summit.response.Result;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.http.converter.json.AbstractJsonHttpMessageConverter;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Implementation of {@link org.springframework.http.converter.HttpMessageConverter} that can read
 * and write JSON using the
 * <a href="https://code.google.com/p/google-gson/">Google Gson</a> library.
 *
 * <p>This converter can be used to bind to typed beans or untyped {@code HashMap}s.
 * By default, it supports {@code application/json} and {@code application/*+json} with {@code
 * UTF-8} character set.
 *
 * <p>Tested against Gson 2.8; compatible with Gson 2.0 and higher.
 *
 * @author Roy Clarkson
 * @author Juergen Hoeller
 * @see com.google.gson.Gson
 * @see com.google.gson.GsonBuilder
 * @see #setGson
 * @since 4.1
 */
public class MyGsonHttpMessageConverter extends AbstractJsonHttpMessageConverter {

    private Gson gson;


    /**
     * Construct a new {@code GsonHttpMessageConverter} with default configuration.
     */
    public MyGsonHttpMessageConverter() {
        this.gson = new Gson();
    }

    /**
     * Construct a new {@code GsonHttpMessageConverter} with the given delegate.
     *
     * @param gson the Gson instance to use
     * @since 5.0
     */
    public MyGsonHttpMessageConverter(Gson gson) {
        Assert.notNull(gson, "A Gson instance is required");
        this.gson = gson;
    }


    /**
     * Set the {@code Gson} instance to use. If not set, a default {@link Gson#Gson() Gson} instance
     * will be used.
     * <p>Setting a custom-configured {@code Gson} is one way to take further
     * control of the JSON serialization process.
     *
     * @see #MyGsonHttpMessageConverter(Gson)
     */
    public void setGson(Gson gson) {
        Assert.notNull(gson, "A Gson instance is required");
        this.gson = gson;
    }

    /**
     * Return the configured {@code Gson} instance for this converter.
     */
    public Gson getGson() {
        return this.gson;
    }


    @Override
    protected Object readInternal(Type resolvedType, Reader reader) throws Exception {
        return getGson().fromJson(reader, resolvedType);
    }

    @Override
    protected void writeInternal(Object object, @Nullable Type type, Writer writer)
        throws Exception {
        // In Gson, toJson with a type argument will exclusively use that given type,
        // ignoring the actual type of the object... which might be more specific,
        // e.g. a subclass of the specified type which includes additional fields.
        // As a consequence, we're only passing in parameterized type declarations
        // which might contain extra generics that the object instance doesn't retain.
        if (type instanceof ParameterizedType) {
            Class<?> rawClazz = (Class<?>) ((ParameterizedType) type).getRawType();
            if (rawClazz.isInterface() || Modifier.isAbstract(rawClazz.getModifiers())) {
                getGson().toJson(object, writer);
                return;
            }
            if (object instanceof Result) {
                Type wrapperType;
                if (Result.class.isAssignableFrom(rawClazz)) {
                    wrapperType = type;
                } else {
                    wrapperType = TypeToken.getParameterized(object.getClass(), type).getType();
                }
                getGson().toJson(object, wrapperType, writer);
            }
        } else {
            getGson().toJson(object, writer);
        }
    }

}
